package com.bmw.newbabytimer;

import android.os.Bundle;
import android.support.v4.content.Loader;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by dss on 14/12/24.
 */
public class GridCounterFragment extends CounterFragement {
    private GridAdapter goodsAdapter = null;
    private GridView goodsGridView = null;

    public static GridCounterFragment newInstance() {
        GridCounterFragment fragment = new GridCounterFragment();
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_content_grid, container,
                false);
        return view;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        goodsGridView = (GridView)view.findViewById(R.id.goods_grid_view);
        goodsGridView.setAdapter(getCounterViewAdapter());
        getLoaderManager().initLoader(0,null, this);
    }

    @Override
    public ViewGroup getCounterView() {
        return goodsGridView;
    }

    @Override
    public GoodsAdapter getCounterViewAdapter() {
        if (null == goodsAdapter) {
            goodsAdapter = new GridAdapter(getActivity());
        }

        return goodsAdapter;
    }

    @Override
    public Loader<List<Goods>> onCreateLoader(int id, Bundle args) {
        return new ThrowableLoader<List<Goods>>(getActivity(), null) {
            @Override
            public List<Goods> loadData() throws Exception {
                return hock();
            }
        };
    }

    private List<Goods> hock() {
        List<Goods> goodslist = new ArrayList<Goods>();

        for (int i = 30; i > 0; i--) {
            Goods goods = new Goods();
            goods.img = "http://imgs-qn.iliangcang.com/ware/topic/cover/1/86.jpg";
            goodslist.add(goods);
        }

        return goodslist;
    }
}
